//
//  GXJTextField.m
//  X15.YiXiuDa
//
//  Created by 轩瑞 on 16/9/21.
//  Copyright © 2016年 NanJing. All rights reserved.
//

#import "GXJTextField.h"

@interface GXJTextField()<UITextFieldDelegate>
@property (nonatomic,strong)UIImageView *pic;
@property (nonatomic,strong)UITextField *textF;
@end

@implementation GXJTextField
- (void)layoutSubviews {
    [super layoutSubviews];
    [self.textF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

-(instancetype)init
{
    self = [super init];
    if(self)
    {
        [self setupView];
    }
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame: frame];
    if(self)
    {
        [self setupViewS];
    }
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame andImage:(NSString *)image
{
    self = [super initWithFrame: frame];
    if(self)
    {
        [self setupView];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame andRightStr:(NSString *)str
{
    self = [super initWithFrame: frame];
    if(self)
    {
        [self setupView];
    }
    return self;
}

- (void) textFieldDidChange:(UITextField *) TextField{
    _text = TextField.text;
    if ([self.delegate respondsToSelector:@selector(GXJtextFieldChangeText:)]) {
        [self.delegate GXJtextFieldChangeText:self];
    }
}

-(void)setupView
{
    _textF = [[UITextField alloc]initWithFrame:CGRectMake(20 * m6Scale +_pic.frame.size.width, 0, self.frame.size.width - 20 * m6Scale -_pic.frame.size.width - 60 * m6Scale, self.frame.size.height) ];
    _textF.delegate = self;
    [self addSubview:_textF];
}

-(void)setupViewS
{
    _textF = [[UITextField alloc]initWithFrame:CGRectMake(20 * m6Scale, 0, self.frame.size.width - 35 * m6Scale, self.frame.size.height) ];
    _textF.delegate = self;
    [self addSubview:_textF];
}

-(void)setFont:(CGFloat)font
{
    _font = font;
    _textF.font = [UIFont systemFontOfSize:_font];
}
-(void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    _textF.placeholder = placeholder;
}

-(void)setImageName:(NSString *)imageName
{
    _imageName = imageName;
    _pic = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, self.frame.size.height-10, self.frame.size.height-10)];
    _pic.image = [UIImage imageNamed:imageName];
    [self addSubview:_pic];
    
    _textF = [[UITextField alloc]initWithFrame:CGRectMake(15+_pic.frame.size.width, 0, self.frame.size.width-10-_pic.frame.size.width, self.frame.size.height)];
    _textF.delegate = self;
    [self addSubview:_textF];
}
-(void)setClear:(BOOL)clear
{
    _clear = clear;
    if(clear)
        _textF.clearButtonMode = UITextFieldViewModeWhileEditing;
    else
        _textF.clearButtonMode = UITextFieldViewModeNever;
}
-(void)setCorcornerRadius:(CGFloat)corcornerRadius
{
    self.layer.cornerRadius = corcornerRadius;
}
-(void)setText:(NSString *)text
{
    _text = text;
    _textF.text = text;
}
-(void)setCornerRadius:(CGFloat)cornerRadius
{
    _cornerRadius = cornerRadius;
    self.layer.cornerRadius = _cornerRadius;
}
-(void)setIsResponse:(BOOL)isResponse
{
    _isResponse = isResponse;
    if(!_isResponse)
    {
        UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
        UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];
        [firstResponder resignFirstResponder];
    }
}
//-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
//{
//    _text = textField.text;
//    return YES;
//}
//-(void)textFieldDidBeginEditing:(UITextField *)textField
//{
//    _text = textField.text;
//}
//-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
//{
//    _text = textField.text;
//    return YES;
//}
//-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    _text = [textField.text stringByAppendingString:string];
//    NSLog(@"++++%@",_text);
//    return YES;
//}
//-(void)textFieldDidEndEditing:(UITextField *)textField
//{
//    _text = textField.text;
//}


@end
